package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.client.VodClient;
import com.atguigu.eduservice.entity.EduVideo;
import com.atguigu.eduservice.service.EduVideoService;
import com.atguigu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author lsx
 * @since 2020-08-01
 */
@RestController
@RequestMapping("/eduservice/video")
@CrossOrigin
public class EduVideoController {
    @Autowired
    private EduVideoService eduVideoService;

    @Autowired
    private VodClient vodClient;

    /**
     * 添加小节
     * @param eduVideo
     * @return
     */
    @PostMapping("/addVideo")
    public  R addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return R.ok();
    }
    @DeleteMapping("{id}")
    public  R deleteVideo(@PathVariable String id){
        //TODO 这里需要拓展  当小节下有视频时 需要将阿里云视频删除才可以删除小节  没有报错 但是远程调用没效果  阿里云视频没有被删除
        //删除阿里云视频需要调用vod模块中的方法 这里使用微服务中的注册中心
        //实现不同的微服务模块之间调用  把这些模块在注册中心中进行注册  注册之后实现互相调用
        //根据小节id获取视频id  然后删除视频
        EduVideo eduVideo = eduVideoService.getById(id);
        //得到视频id
        String videoSourceId = eduVideo.getVideoSourceId();
        //删除之前先校验视频id是否为null
        if(!StringUtils.isEmpty(videoSourceId)){
            //使用feign远程服务调用
            R result = vodClient.removeVideoById(videoSourceId);
            /*if(result.getCode()==20001){
                throw  new GuliException(20001,"执行熔断器");
            }*/
        }
        eduVideoService.removeById(id);
        return  R.ok();
    }
    @GetMapping("/getVideoById/{id}")
    public  R getVideoById(@PathVariable String id){
        EduVideo eduVideo = eduVideoService.getById(id);
        return  R.ok().data("eduVideo",eduVideo);
    }
    @PostMapping("/updateVideo")
    public  R updateVideo(@RequestBody EduVideo eduVideo){
       eduVideoService.updateById(eduVideo);
        return R.ok();
    }

}

